﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace QQ2564874169.Core.ORM.Sqlserver
{
    public class MssqlExecute : DbExecute
    {
        public MssqlExecute(ISql sql) : base(sql)
        {
        }

        protected override IDbEntityQuery GetEntityQuery(ISql sql)
        {
            return new MssqlEntityQuery(sql);
        }

        protected override ExecuteParam OnInsertSql(EntityParam context)
        {
            if (context.Paramters.Count < 1)
                throw new ArgumentException("解析SQL失败，没有可新增的内容。");
            var left = string.Join(",", context.Paramters.Keys.Select(i => $"[{i}]"));
            var right = string.Join(",", context.Paramters.Keys.Select(i => $"@{i}"));

            var param = new ExecuteParam
            {
                Command = $"INSERT INTO [{context.SqlName}]({left}) VALUES({right})",
            };
            param.Parameters.Add(context.Paramters);
            return param;
        }

        protected override ExecuteParam OnDeleteSql(EntityParam context)
        {
            var where = string.Join(" AND ", context.Paramters.Keys.Select(k => $"[{k}]=@{k}"));
            if (context.Paramters.Count > 0)
                where = " WHERE " + where;
            var param = new ExecuteParam
            {
                Command = $"DELETE FROM [{context.SqlName}]{where}"
            };
            param.Parameters.Add(context.Paramters);
            return param;
        }

        protected override ExecuteParam OnUpdateSql(EntityParam sets, EntityParam where)
        {
            if (sets.Paramters.Count < 1)
                throw new ArgumentException("没有可更新的字段");

            var map = new Dictionary<string, object>();
            var setsql = string.Join(",", sets.Paramters.Keys.Select(k => $"[{k}]=@s_{k}"));
            foreach (var key in sets.Paramters.Keys)
            {
                map.Add("s_" + key, sets.Paramters[key]);
            }

            var wheresql = string.Join(" AND ", where.Paramters.Keys.Select(k => $"[{k}]=@w_{k}"));
            if (where.Paramters.Count > 0)
            {
                wheresql = " WHERE " + wheresql;
                foreach (var key in where.Paramters.Keys)
                {
                    map.Add("w_" + key, where.Paramters[key]);
                }
            }
            var param = new ExecuteParam
            {
                Command = $"UPDATE [{where.SqlName}] SET {setsql}{wheresql}"
            };
            param.Parameters.Add(map);
            return param;
        }

        protected override ExecuteParam OnSetNullSql(string[] sets, EntityParam where)
        {
            if (sets.Length < 1)
                throw new ArgumentException("没有可更新的字段");
            
            var setsql = string.Join(",", sets.Select(k => $"[{k}]=NULL"));
            var wheresql = string.Join(" AND ", where.Paramters.Keys.Select(k => $"[{k}]=@{k}"));
            if (where.Paramters.Count > 0)
            {
                wheresql = " WHERE " + wheresql;
            }
            var param = new ExecuteParam
            {
                Command = $"UPDATE [{where.SqlName}] SET {setsql}{wheresql}"
            };
            param.Parameters.Add(where.Paramters);
            return param;
        }

        protected override ExecuteParam OnProcSql(EntityParam context)
        {
            var param = new ExecuteParam
            {
                Command = context.SqlName,
                IsProc = true
            };
            param.Parameters.Add(context.Paramters);
            return param;
        }
    }
}
